package com.ch.forum.controller;

import com.alibaba.fastjson.JSON;
import com.ch.forum.bo.ResponseResult;
import com.ch.forum.dao.ArticelMapper;
import com.ch.forum.model.ArtModel;
import com.ch.forum.service.ArticleService;
import com.ch.forum.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/art")
@Controller
public class ArticleController {

    public static final String ADD_PAGE = "art/add";

    public static final String UPDATE_PAGE = "art/update";

    @Autowired
    private ArticleService articleService;

    @GetMapping(value = "add")
    public String add(){
        return ADD_PAGE;
    }

    @GetMapping(value = "update")
    public String update(){
        return UPDATE_PAGE;
    }

    @GetMapping(value = "/{pageNo}/{pageSize}")
    @ResponseBody
    public ResponseResult page(@PathVariable("pageNo") int pageNo,
                              @PathVariable("pageSize") int pageSize){
        return articleService.findArtList(pageNo, pageSize);
    }

    @GetMapping()
    @ResponseBody
    public ResponseResult allList(){
        return articleService.findArtList(0, 0);
    }

    @GetMapping("/latest/{pageNo}/{pageSize}")
    @ResponseBody
    public ResponseResult latest(@PathVariable("pageNo") int pageNo,
                                 @PathVariable("pageSize") int pageSize){
        return articleService.findLatestPage(pageNo, pageSize);
    }

    @GetMapping("/search/{key}/{pageNo}/{pageSize}")
    @ResponseBody
    public ResponseResult search(@PathVariable("pageNo") int pageNo,
                                 @PathVariable("key") String key,
                                 @PathVariable("pageSize") int pageSize){
        return articleService.findSearchKeyWordsPage(pageNo,pageSize, StringUtils.isEmpty(key) ? "" : key);
    }

    @PostMapping()
    @ResponseBody
    public ResponseResult addArt(ArtModel model){
        return articleService.addArticele(model);
    }

    @GetMapping("{id}")
    @ResponseBody
    public ResponseResult findById(@PathVariable Long id){
        return articleService.findArtById(id);
    }

    @GetMapping("/my/{userId}")
    @ResponseBody
    public ResponseResult findMyArtListByUserId(@PathVariable("userId")Long userId,
                                                @RequestParam("pageNo") int pageNo,
                                                @RequestParam("pageSize") int pageSize){
        return articleService.findArtListByUserId(userId, pageNo, pageSize);
    }

    @GetMapping("/zan")
    @ResponseBody
    public ResponseResult zanAdd(@RequestParam("artId") Long artId, @RequestParam("userId") Long userId){
        return articleService.addZanNum(artId, userId);
    }

    @GetMapping("/zan-cancel")
    @ResponseBody
    public ResponseResult zanCancel(@RequestParam("artId") Long artId, @RequestParam("userId") Long userId){
        return articleService.zanCancel(artId, userId);
    }


}
